<template>
  <el-dialog
    :model-value="visible"
    width="560px"
    title="修改有效期"
    @close="handleClose"
    :fullscreen="device === 'mobile'"
  >
    <el-form :model="form" ref="formRef" label-width="100px">
      <slot :form="form"></slot>
      <el-form-item label="有效期" required>
        <el-row>
          <el-col :span="11">
            <el-form-item label-width="0" prop="startDate" :rules="[{ required: true, message: '请选择开始时间', trigger: 'blur'}]">
              <el-date-picker
                v-model="form.startDate"
                :editable="false"
                type="datetime"
                placeholder="请选择开始日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <div style="text-align: center;">-</div>
          </el-col>
          <el-col :span="11">
            <el-form-item label-width="0" prop="endDate" :rules="[{ required: true, message: '请选择结束时间', trigger: 'blur'}]">
              <el-date-picker
                v-model="form.endDate"
                type="datetime"
                :editable="false"
                placeholder="请选择结束日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleOk">
        确认
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup name="EditExpirationDate">
import useAppStore from '@/store/modules/app'

const { proxy } = getCurrentInstance()

const emit = defineEmits()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  value: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const data = reactive({
  form: {}
})
const { form } = toRefs(data)
const device = computed(() => useAppStore().device)
watch(() => props.visible, (val) => {
  if (val) form.value = { ...props.value }
})

// 关闭弹窗
function handleClose() {
  emit('cancel')
}
// 确认事件
function handleOk() {
  proxy.$refs['formRef'].validate(valid => {
    if (valid) {
      emit('ok', form.value)
    }
  })
}
</script>
